package com.kwedding.service.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.kwedding.bean.TB_Media;
import com.kwedding.dao.TB_MediaMapper;
import com.kwedding.dto.web.RequestWebAddBrandMedia;
import com.kwedding.dto.web.RequestWebMediaList;
import com.kwedding.dto.web.RequestWebUpDownBrandMedia;
import com.kwedding.dto.web.RequestWebUpdateBrandMedia;
import com.kwedding.dto.web.ResponseWebMediaList;
import com.kwedding.util.CheckedUtil;
import com.kwedding.util.Constants;
import com.kwedding.util.CopyPageInfo;
import com.kwedding.util.CopyerObject;
import com.kwedding.util.GsonUtil;
import com.kwedding.util.IDGenerate;
import com.kwedding.util.LoggerParent;
import com.kwedding.util.RequestErrorBackException;
import com.kwedding.util.Resp_PageInfo;

import springfox.documentation.spring.web.json.Json;

@Service
public class WebMediaService extends LoggerParent {

	@Autowired
	private TB_MediaMapper tb_MediaMapper;

	/**
	 * 根据品牌获取对应视频列表
	 * 
	 * @param webMediaList
	 */
	@Transactional(readOnly = true)
	public Resp_PageInfo<ResponseWebMediaList> getBrandMediaList(RequestWebMediaList webMediaList) {
		CheckedUtil.isNull(webMediaList.getBrandId(), Constants.REQUEST_ERROR_1, "品牌不能为空");
		int page = 1;
		if (webMediaList.getPageNum() != null) {
			page = webMediaList.getPageNum();
		}
		PageHelper.startPage(page, Constants.PAGESIZE);
		List<TB_Media> tb_Medias = tb_MediaMapper.selectMediaList(webMediaList.getStatus(), webMediaList.getBrandId());
		List<ResponseWebMediaList> responseWebMediaLists = new ArrayList<>();
		if (tb_Medias != null && tb_Medias.size() > 0) {
			tb_Medias.stream().forEach(r -> {
				ResponseWebMediaList mediaList = CopyerObject.copyerObject(r, ResponseWebMediaList.class);
				mediaList.setIdStr(r.getMediaId().toString());
				responseWebMediaLists.add(mediaList);
			});
		}
		return CopyPageInfo.copyPageInfo(tb_Medias, responseWebMediaLists);
	}

	/**
	 * 添加品牌视频
	 * 
	 * @param addBrandMedia
	 */
	@Transactional
	public void insertBrandMedia(RequestWebAddBrandMedia addBrandMedia) {
		CheckedUtil.isNull(addBrandMedia.getBrandId(), Constants.REQUEST_ERROR_1, "品牌Id不能为空");
		// CheckedUtil.isNull(addBrandMedia.getTitle(), Constants.REQUEST_ERROR_2,
		// "标题不能为空");
		// CheckedUtil.isNull(addBrandMedia.getImages(), Constants.REQUEST_ERROR_3,
		// "图片不能为空");
		CheckedUtil.isNull(addBrandMedia.getType(), Constants.REQUEST_ERROR_4, "类型不能为空");
		CheckedUtil.isNull(addBrandMedia.getUrl(), Constants.REQUEST_ERROR_5, "视频地址不能为空");
		TB_Media tb_Media = tb_MediaMapper.selectMediaInUse(addBrandMedia.getBrandId(), addBrandMedia.getType());
		if (tb_Media != null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_6, "该品牌同类型视频已存在");
		}
		tb_Media = CopyerObject.copyerObject(addBrandMedia, TB_Media.class);
		tb_Media.setMediaId(IDGenerate.generate());
		tb_Media.setCreateTime(new Date());
		tb_Media.setStatus(false);
		if (addBrandMedia.getImages() != null) {
			tb_Media.setImages(new Json(GsonUtil.listToJsonStr(addBrandMedia.getImages())));
		}
		tb_Media.setUpdateTime(new Date());
		if (tb_MediaMapper.insert(tb_Media) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_7, "添加品牌视频失败");
		}

	}

	/**
	 * 修改视频
	 * 
	 * @param updateBrandMedia
	 */
	@Transactional
	public void updateBrandMedia(RequestWebUpdateBrandMedia updateBrandMedia) {
		CheckedUtil.isNull(updateBrandMedia.getMediaId(), Constants.REQUEST_ERROR_1, "请先选择要修改的内容");
		CheckedUtil.isNull(updateBrandMedia.getTitle(), Constants.REQUEST_ERROR_2, "标题不能为空");
		// CheckedUtil.isNull(updateBrandMedia.getImages(), Constants.REQUEST_ERROR_3,
		// "图片不能为空");
		CheckedUtil.isNull(updateBrandMedia.getType(), Constants.REQUEST_ERROR_4, "类型不能为空");
		CheckedUtil.isNull(updateBrandMedia.getUrl(), Constants.REQUEST_ERROR_5, "视频地址不能为空");
		TB_Media tb_Media = tb_MediaMapper.selectByPrimaryKey(updateBrandMedia.getMediaId());
		if (tb_Media == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_6, "要修改的内容不存在或已删除");

		}
		tb_Media = CopyerObject.copyerObject(updateBrandMedia, TB_Media.class);
		tb_Media.setUpdateTime(new Date());
		if (updateBrandMedia.getImages() != null) {
			tb_Media.setImages(new Json(GsonUtil.listToJsonStr(updateBrandMedia.getImages())));
		}
		if (tb_MediaMapper.updateSelective(tb_Media) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_7, "修改视频内容失败");
		}
	}

	/**
	 * 上下架品牌视频
	 * 
	 * @param upDownBrandMedia
	 */
	@Transactional
	public void upDownBrandMedia(RequestWebUpDownBrandMedia upDownBrandMedia) {
		CheckedUtil.isNull(upDownBrandMedia.getMediaId(), Constants.REQUEST_ERROR_1, "请先选择要修改的内容");
		TB_Media tb_Media = tb_MediaMapper.selectByPrimaryKey(upDownBrandMedia.getMediaId());
		if (tb_Media == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_2, "要修改的内容不存在或已删除");
		}
		tb_Media.setStatus(!tb_Media.getStatus());
		tb_Media.setUpdateTime(new Date());
		if (tb_MediaMapper.updateSelective(tb_Media) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_3, "修改视频内容失败");
		}
	}
}
